/**
 * yun core js
 */

// make sure yun namespace is not conflict with other javascript.
if (yun) {
  alert("system tampered, yun js variable already exsit");
  throw "namespace conflict with yun";
}
else {
  //setup yun namespace
  var yun = {};
}
/**
 * yun language module
 * support 'en', 'gb2312' only
 */
yun.language = {
  current_language : 'en',
  //initialize language switcher
  init : function(){
    /** if set language not available, hide language switcher*/
    if(typeof(set_language) != 'function'){
    	$("#language_selector").addClass("hidden");
    }
    /** or else add click event to langauge switcher */
    else{
      $("#language_selector").click(function(){
      	if(yun.language.current_language === 'gb2312'){
      	  $("#language_selector").removeClass("gb2312").html("English");
      	  yun.language.current_language = 'en';
      	}
      	else if(yun.language.current_language === 'en'){
      	  $("#language_selector").addClass("gb2312").html("简体中文");	
      	  yun.language.current_language = 'gb2312';
      	}
      	/** page specific function to translate langauge */
      	if(typeof(set_language) == 'function')
      	{
      		set_language(yun.language.current_language);
      	}
      });
    }
  }
}
//invoke language initialization
$(document).ready(function(){
  yun.language.init();
});
/** ----------------------------------------------------------------------------
/**
 * yun button effect helper to make button look more natural
 * just for the effect hack.
 */
yun.buttonHelper = {
  active : "",
  makeUnselectable : function(){
    $(".button").bind("selectstart", function(){return false;});	
  }
}
/**
 * implement button helper when document loaded
 * 1. make text unselectable for IE9
 * 2. improve button effect
 */
$(document).ready(function(){
  var url_base = $("base").attr("href"); 
  yun.buttonHelper.makeUnselectable();
  $(".button").mousedown(function(){
    yun.buttonHelper.active = $(this).attr("link");
  });

  $(".button").mouseup(function(){
    if($(this).attr("link") === yun.buttonHelper.active && $(this).attr("link") !== undefined){
      location.href = url_base + "index.php/" + $(this).attr("link");
    }
  });
});
/** ----------------------------------------------------------------------------

/** global helper function behave same as print_r() in PHP */
yun.print_r = function(theObj)
{
	if(theObj.constructor == Array || theObj.constructor == Object)
	{
		document.write("<ul>");
		for(var p in theObj)
		{
			if(theObj[p].constructor == Array|| theObj[p].constructor == Object)
			{
				document.write("<li>["+p+"] => "+typeof(theObj)+"</li>");
				document.write("<ul>");
				print_r(theObj[p]);
				document.write("</ul>");
			}
			else 
			{
				document.write("<li>["+p+"] => "+theObj[p]+"</li>");
			}
		}
		document.write("</ul>");
	}
}

/** ----------------------------------------------------------------------------

/* End of file yun.js */
/* Location: ~/js/yun.js */
